class: center, middle, inverse, title-slide .title[ # Phase II: Using Our Toolbox ] .subtitle[ ## Module 7: Birds of a Feather ] .author[ ### Dr. Christopher Kenaley ] .institute[ ### Boston College ] .date[ ### 2024/14/11 ] --- class: inverse, top # In class today ``` ## Warning: package 'ggplot2' was built under R version 4.2.3 ``` <!-- Add icon library --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"> .pull-left[ Today we'll .... - Explore the logistic curve  ] .pull-right[ <iframe width="460" height="315" src="https://www.youtube.com/embed/d5gf9dXbPi0?si=A6-tszCKpzgdF6nU" frameborder="0" allowfullscreen></iframe> ] --- class: inverse, top <!-- slide 1 --> ## Module 8: Birds of a feather  --- class: inverse, top <!-- slide 1 --> ## Module 8: Birds of a feather ``` r yr <- c(2021,2022,2023) bhv_l <- list() for(i in yr){ bhv_l[[i]] <- occ_data(scientificName = "Vireo solitarius", year=i, month="3,6", limit=1000, country="US", basisOfRecord = "HUMAN_OBSERVATION", stateProvince="Massachusetts")[[2]] %>% select(individualCount, year,month,day, decimalLongitude, decimalLatitude) } bhv <- do.call(rbind, bhv_l) ``` --- class: inverse, top <!-- slide 1 --> ## Module 8: Birds of a feather .pull-left[ ``` r mass <- ne_states(country = "United States of America", returnclass = "sf") %>% filter(name=="Massachusetts") ``` ] .pull-right[ ``` r mass %>% ggplot() + geom_sf()+ geom_point(data=bhv, aes(decimalLongitude, decimalLatitude, col=year)) ``` <!-- --> ] -- class: inverse, top <!-- slide 1 --> ## Module 8: Birds of a feather .pull-left[ ### The logistic curve  ] -- class: inverse, top <!-- slide 1 --> ## Module 8: Birds of a feather .pull-left[ ### The logistic curve ``` r bhv_arrive <- bhv%>%mutate(n=1:n()) %>% group_by(n) %>% mutate(date=as.Date(paste0(year,"-",month,"-",day)), j.day=julian(date, origin=as.Date(paste0(unique(year),"-01-01"))) )%>% na.omit() %>% group_by(year,j.day,date)%>% reframe(day.tot=sum(individualCount,na.rm=T))%>% group_by(year)%>% mutate(prop=cumsum(day.tot/sum(day.tot,na.rm = T))) ``` ] .pull-right[ ``` r bhv_arrive %>% ggplot(aes(j.day,prop,col=as.factor(year))) + geom_point() ``` <!-- --> --- class: inverse, top <!-- slide 1 --> ## Module 8: Birds of a feather .pull-left[ ### The logistic curve ``` r bhv_pred <- bhv_arrive%>% group_by(year)%>% reframe( pred=predict(nls(prop~SSlogis(j.day,Asym, xmid, scal)),newdata=data.frame(j.day=min(j.day):max(j.day))), j.day=min(j.day):max(j.day), )%>% left_join(bhv_arrive%>%dplyr::select(j.day,date,prop)) ``` ] .pull-right[ non-linear least squares using `nls` package `SSlogis`: - vector `input` * - asymptote `Asym`* - inflection point `xmid` * - scale `scale` * ``` r SSlogis(input,Asym, xmid, scal) ``` * functions that compute these values ] --- class: inverse, top <!-- slide 1 --> ## Module 8: Birds of a feather .pull-left[ ### The logistic curve ``` r p <- bhv_pred %>% ggplot(aes(j.day,prop),alpha=0.3) + geom_point()+ geom_line(aes(j.day,pred,group=year),col="blue") ``` ] .pull-left[ ``` ## Warning: Removed 4 rows containing missing values or values outside the scale range ## (`geom_point()`). ``` <!-- --> ]